How to make global variables that are contained in a different class thread safe in c
up vote
2
down vote
favorite
I have to files one called main.c and one called disperse.c
disperse.c creates threads to handle the load in main.c and looks something like this:
void *entry(void *arg) {
foo_function()
}
void disperse() {
pthread_t thread;
pthread_create(&thread, NULL, entry, (void*) args);
pthread_join(thread, NULL);
}
main.c contains foo_function() and the function edits global variables.
Is there any way to make the global variables contained in main.c thread safe?
c multithreading thread-safety
add a comment |
up vote
2
down vote
favorite
I have to files one called main.c and one called disperse.c
disperse.c creates threads to handle the load in main.c and looks something like this:
void *entry(void *arg) {
foo_function()
}
void disperse() {
pthread_t thread;
pthread_create(&thread, NULL, entry, (void*) args);
pthread_join(thread, NULL);
}
main.c contains foo_function() and the function edits global variables.
Is there any way to make the global variables contained in main.c thread safe?
c multithreading thread-safety
It makes no sense to create a new thread and thenjoin
it on the very next line. Why bother to create the thread? Why not just callentry()
at that point?
– Solomon Slow
Nov 19 at 16:50
I think this question would be better on softwareengineering.stackexchange.com
– Broman
Nov 19 at 16:50
Use thread local storage en.wikipedia.org/wiki/Thread-local_storage#C_and_C++
– sturcotte06
Nov 19 at 16:51
@Broman when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 19 at 20:48
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have to files one called main.c and one called disperse.c
disperse.c creates threads to handle the load in main.c and looks something like this:
void *entry(void *arg) {
foo_function()
}
void disperse() {
pthread_t thread;
pthread_create(&thread, NULL, entry, (void*) args);
pthread_join(thread, NULL);
}
main.c contains foo_function() and the function edits global variables.
Is there any way to make the global variables contained in main.c thread safe?
c multithreading thread-safety
I have to files one called main.c and one called disperse.c
disperse.c creates threads to handle the load in main.c and looks something like this:
void *entry(void *arg) {
foo_function()
}
void disperse() {
pthread_t thread;
pthread_create(&thread, NULL, entry, (void*) args);
pthread_join(thread, NULL);
}
main.c contains foo_function() and the function edits global variables.
Is there any way to make the global variables contained in main.c thread safe?
c multithreading thread-safety
c multithreading thread-safety
asked Nov 19 at 16:25
Django
133
133
It makes no sense to create a new thread and thenjoin
it on the very next line. Why bother to create the thread? Why not just callentry()
at that point?
– Solomon Slow
Nov 19 at 16:50
I think this question would be better on softwareengineering.stackexchange.com
– Broman
Nov 19 at 16:50
Use thread local storage en.wikipedia.org/wiki/Thread-local_storage#C_and_C++
– sturcotte06
Nov 19 at 16:51
@Broman when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 19 at 20:48
add a comment |
It makes no sense to create a new thread and thenjoin
it on the very next line. Why bother to create the thread? Why not just callentry()
at that point?
– Solomon Slow
Nov 19 at 16:50
I think this question would be better on softwareengineering.stackexchange.com
– Broman
Nov 19 at 16:50
Use thread local storage en.wikipedia.org/wiki/Thread-local_storage#C_and_C++
– sturcotte06
Nov 19 at 16:51
@Broman when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 19 at 20:48
It makes no sense to create a new thread and then
join
it on the very next line. Why bother to create the thread? Why not just call entry()
at that point?– Solomon Slow
Nov 19 at 16:50
It makes no sense to create a new thread and then
join
it on the very next line. Why bother to create the thread? Why not just call entry()
at that point?– Solomon Slow
Nov 19 at 16:50
I think this question would be better on softwareengineering.stackexchange.com
– Broman
Nov 19 at 16:50
I think this question would be better on softwareengineering.stackexchange.com
– Broman
Nov 19 at 16:50
Use thread local storage en.wikipedia.org/wiki/Thread-local_storage#C_and_C++
– sturcotte06
Nov 19 at 16:51
Use thread local storage en.wikipedia.org/wiki/Thread-local_storage#C_and_C++
– sturcotte06
Nov 19 at 16:51
@Broman when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 19 at 20:48
@Broman when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 19 at 20:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
In general, it is best to avoid global variables unless you absolutely cannot do what you are trying to do otherwise. With pthreads, thread safety is dependent on the function. Not all of the pthreads functions are "thread safe" on shared data. You can also use thread mutexes to protect shared data. These are essentially a type of lock on a piece of shared data, that only allows one thread to access it at a time. This article gives a good introduction to this: https://randu.org/tutorials/threads/#protect
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
In general, it is best to avoid global variables unless you absolutely cannot do what you are trying to do otherwise. With pthreads, thread safety is dependent on the function. Not all of the pthreads functions are "thread safe" on shared data. You can also use thread mutexes to protect shared data. These are essentially a type of lock on a piece of shared data, that only allows one thread to access it at a time. This article gives a good introduction to this: https://randu.org/tutorials/threads/#protect
add a comment |
up vote
0
down vote
accepted
In general, it is best to avoid global variables unless you absolutely cannot do what you are trying to do otherwise. With pthreads, thread safety is dependent on the function. Not all of the pthreads functions are "thread safe" on shared data. You can also use thread mutexes to protect shared data. These are essentially a type of lock on a piece of shared data, that only allows one thread to access it at a time. This article gives a good introduction to this: https://randu.org/tutorials/threads/#protect
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
In general, it is best to avoid global variables unless you absolutely cannot do what you are trying to do otherwise. With pthreads, thread safety is dependent on the function. Not all of the pthreads functions are "thread safe" on shared data. You can also use thread mutexes to protect shared data. These are essentially a type of lock on a piece of shared data, that only allows one thread to access it at a time. This article gives a good introduction to this: https://randu.org/tutorials/threads/#protect
In general, it is best to avoid global variables unless you absolutely cannot do what you are trying to do otherwise. With pthreads, thread safety is dependent on the function. Not all of the pthreads functions are "thread safe" on shared data. You can also use thread mutexes to protect shared data. These are essentially a type of lock on a piece of shared data, that only allows one thread to access it at a time. This article gives a good introduction to this: https://randu.org/tutorials/threads/#protect
answered Nov 19 at 16:32
jamie schnaitter
846
846
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378866%2fhow-to-make-global-variables-that-are-contained-in-a-different-class-thread-safe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It makes no sense to create a new thread and then
join
it on the very next line. Why bother to create the thread? Why not just callentry()
at that point?– Solomon Slow
Nov 19 at 16:50
I think this question would be better on softwareengineering.stackexchange.com
– Broman
Nov 19 at 16:50
Use thread local storage en.wikipedia.org/wiki/Thread-local_storage#C_and_C++
– sturcotte06
Nov 19 at 16:51
@Broman when referring other sites, it is often helpful to point that cross-posting is frowned upon
– gnat
Nov 19 at 20:48