LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2014, 06:53 PM   #1
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Rep: Reputation: 15
Thread spawning with the same function


Hi,

I want to create multiple threads, but all the thread should be spawned using the same function. If I use the same function name for all the threads during pthread_create, then will each thread have its own copy of the function?

Thanks,
D
 
Old 02-07-2014, 09:36 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
No. It is the same code. But because each function allocates its own stack frame, the data is local to each thread.

That is how it should be done. As soon as you use static variables your function is not "thread-safe" in most cases. If you access global variables in your function might not be thread safe. You have to take special precautions to keep it thread safe.

jlinkels
 
Old 02-08-2014, 01:46 PM   #3
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks for the reply. My function have local variables and one global. So if I use some locking mechanism for the global variable, am I ok?

Thanks,
D
 
Old 02-09-2014, 08:19 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
If your local variables are not static, yes.

"Some locking" mechanism is a somewhat sloppy term. You have to use a carefully designed, well thought locking policy. The problem is not so much in protecting the integrity of the variables, but in the avoidance of lock conditions. There are a couple of standard functions in the thread library which deal with locking, like semaphores.

I am not opposed to threads at all, but some people are and describe the problems they can cause:
http://www.eecs.berkeley.edu/Pubs/Te...ECS-2006-1.pdf
And Google will turn up many more documents.

jlinkels
 
Old 02-10-2014, 02:06 AM   #5
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks again. One more clarification. If I pass a pointer to a same structure to all the threads that I spawn, will the changes
to the struct in one thread be reflected on the other thread, or a local copy of the pointer is created for each thread?

Thanks,
D
 
Old 02-10-2014, 02:11 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,500

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
just imagine, the local variables belong to the current thread, global variables are accessible from every thread. Pointers will point to a "global" memory, therefore it will be common for all the threads. (The pointer itself can be local).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 12:07 PM
kill thread which is with block function... (fgets...) okeyla Programming 2 10-14-2009 08:32 PM
c system function in child thread CoderMan Programming 6 04-10-2009 07:41 AM
getting the return value from a thread start function cynthia_thomas Programming 1 02-18-2006 09:23 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 06:03 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration