05-19-2008
Pthread_create issue
Hello
My problem goes like this:
I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code.
Any help will be highly appreciated.
please note that the code does compile but actually does nothing.
my code is:
================================================
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "semaphore.h"
#pragma warning(disable:4996)
//Disable c4496 warning, regarding the potentially unsafe
//methods in the Standard C++ Library.
int arr[5] = {0};
int i;
int j = 0;
int init = 1;
void* manufacturer(void* Thread_data)
{
for (i = init; i <= 100; i++)
{
arr[i-1] = i;
j++;
if (j == 4)
{
down(&semaphore1);
j = 0;
init = i;
up(&semaphore2);
}
}
pthread_exit(NULL);
}
void* consumer(void* Thread_data)
{
if (arr[0] != 0)
{
for (i; i >= 0; i--)
{
if (arr[i] != 0)
{
printf("%d",arr[i]);
arr[i] = 0; //Removing the number from the array.
}
}
}
else
{
down(&semaphore2);
up(&semaphore1);
}
pthread_exit(NULL);
}
int main()
{
int status; //Used to recieve the result of the thread creation.
init_sem(&semaphore1, 0);
init_sem(&semaphore2, 0);
pthread_t manufacturer_thread; //The manufacturer thread's name. pthread_t consumer_thread; //The consumer thread's name.
status = pthread_create(&manufacturer_thread, NULL, manufacturer, NULL);
if (status == 0)
{
printf("The manufacturer process has been successfully created\n");
}
else
{
printf("Error: The manufacturer process could not be created\n");
}
status = pthread_create(&consumer_thread, NULL, consumer, NULL);
if (status == 0)
{
printf("The consumer process has been successfully created\n");
}
else
{
printf("Error: The manufacturer process could not be created\n");
}
}
==========================================================
I know it's a little long, But I just dont know what to do. Im using the knoppix CD( im booting from it) so its under Linux OS.
when I try to run the application, I get:
The manufacturer process has been successfully created
The consumer process has been successfully created
and that's it! meaning that the thread has been created successfully, but the functions are never called..
thanks
9 More Discussions You Might Find Interesting
1. Programming
Here is simple code for multithreading in POSIX:
void* simplethread(void* arg)
{
printf("Hello World\n");
}
int main(void)
{
pthread_t id;
pthread_create(&id, NULL, simplethread, NULL);
return 0;
}
Whether the new thread will run or not depends on the OS. Tricky ... (5 Replies)
Discussion started by: _rocky
5 Replies
2. Programming
how to do with that?
after cc -o xxxx xxxx.c
ld:
Unresolved:
_pthread_create
_pthread_deteach
_pthread_exit
Thanks (3 Replies)
Discussion started by: zhshqzyc
3 Replies
3. Programming
I have a problem with a C multi-threaded program I am writing. I cannot figure out how to keep the unique key value at the thread level. I wrote a program in C that forked a bunch of processes and then decided to convert it to threads and I can't keep the key unique to each thread. In a nutshell... (3 Replies)
Discussion started by: jenmead
3 Replies
4. Programming
Hi.
I use C++ and I wishes to create a thread with the pthread_create function, my question is, how can I do this if I wish that the function will be a member of the class ??
I know from windows programming that I can declare a static function like this
static unsigned int __stdcall... (7 Replies)
Discussion started by: shvalb
7 Replies
5. Solaris
Im trying to run an application i compiled (iperf) and i get an error telling me that it cant create the pthread. when i ran the ./configuration one of the things it checked was for pthreads which came back ok.
Im not really sure where to even start to resolve this. i have been unable to find... (5 Replies)
Discussion started by: jrich523
5 Replies
6. Programming
Hi guys.
H was learning posix threads in C with anjuta IDE. it gives me
undefined reference to `pthread_create'
I know i should compile it like:
gcc -lpthread main.c
how should i import this configuration in anjuta so i can compile inside it? (2 Replies)
Discussion started by: majid.merkava
2 Replies
7. Programming
The prototype for pthread_create function is like this:-
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void *(*start routine),void *arg);
Q.1 .Why the return type of the start_routine must be void*??
Q.2. Why should we pass arg by converting into void * only ??
Thank You (3 Replies)
Discussion started by: sunil_abhay
3 Replies
8. UNIX for Dummies Questions & Answers
Hi,
I'm trying to do my homework assignment but I am having trouble using the pthread_create fucntion.
Here is my code________________
//Alicia Johnson
//sum_pid program
//creates n number of threads. These threads create a random number
//then adds the number to a global array. Then... (1 Reply)
Discussion started by: ajohns38
1 Replies
9. Programming
Suppose I have a simple program main() with a global varibale int x=0.
int x = 0;
main()
{
print("%d\n",x);
}
I want to create two threads/process which must access this variable x in sync.
Which one will be better threads( pthread_create ) or process( fork )?
If I go with fork() then... (1 Reply)
Discussion started by: rupeshkp728
1 Replies