views:

47

answers:

2

I have a method which is is meant to write information into a struct. I want to make it run as a thread.

If I call it by itself, as childWriter((void*) &sa) it works.

If I call pthread_create(&writerChild, NULL, childWriter, (void*) &sa), it no longer works. It doesn't write to the shared object.

This is driving me mad. Why isn't it working, and how do I make it work?

+3  A: 

What makes you so sure that the code doesn't execute? Note that if you do something like:

int main(int argc, char* argv[]) 
{
    pthread_create(....);
    return 0;
}

In the above, the program will quit right away, because the program exits as soon as the main thread has terminated. You need to "join" the thread (using pthread_join), in order to wait for the thread to have terminated. Note that spawning a thread and then joining it is actually worse than simply running the content that the thread would run (since spawning and then joining a thread is equivalent to running the content serially, plus it adds the overhead of the spawn/join). If you intend to multithread things, typically one spawns multiple threads and then either detaches them or joins them later.

Also, you should be cautious about sharing data between threads that can be modified; any object that is read from multiple threads and is modified in even one thread requires explicit locking around access to that object. Otherwise, you can end up with all sorts of garbage and corruption.

Michael Aaron Safyan
A: 

Short version:

Use pthread_join() before you read the data from the struct:

pthread_create(&writerChild, NULL, childWriter, (void*) &sa);

//if necessary, do some other tasks, not related to the struct, here

pthread_join(writerChild,NULL);

//now, you may read from the struct

If you are creating the thread in one function, & reading the struct in another function, simply shift the pthread_join statement to the latter, just before you read from the struct. (Also make sure that the pthread_t variable writerChild is visible in the reading function's scope)

Long version:

Threads are generally used in programs where tasks can be parallelized. I suppose your intention here is to read the data written to the struct after the childWriter function writes to it.

When you call your function in a single-threaded process, via:

childWriter((void*) &sa);

the thread / process shifts to execute the instructions that are part of your function. Only when the function returns, does the execution control return to the point from which you called the function. Hence, you can be sure that childWriter has completed its execution, before you begin to read from your struct in the calling function.

When you create a new thread, via:

pthread_create(&writerChild, NULL, childWriter, (void*) &sa);

the thread runs in parallel with your "main" thread. There is no guarantee that the newly created thread will get a chance to execute before the next instructions in the main thread get exectued, leave alone the possibilty that the childWriter thread function completes its execution prior to reading the struct.

Hence, you need to wait for the thread to complete its execution. This can be accomplished using pthread_join().

Kedar Soparkar