By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

How to make two simultaneously running infinite whiles in C?

P: 2
How to make TWO simultaneously running infinite whiles in C?

If I do this in JAVA:
public static void main(String[] args)
{
new Thread(new Runnable()
{
@Override
public void run()
{
while (true)
{
// some code ...
}

}).start();

while (true)
{
// some code ...
}

}

or in PYTHON:
def thread():
while True:
# some code

def main():
tid = threading.Thread(target = thread)
tid.start()

while True:
# some code

if __name__ == '__main__':
main()

all OK, but when I do this in C:
void *thread(void *args)
{
while (1)
{
// some code
}
}

int main()
{
pthread_t tid;

pthread_create(&tid, NULL, thread, NULL);
pthread_join(tid, NULL);

while (1)
{
// some code
}

return 0;
}

only the loop in the thread runs and the compiler simply does not read the code after creating the thread..
Jun 21 '17 #1
Share this Question
Share on Google+
1 Reply


P: 2
Found the problem:
You need to remove pthread_join (tid, NULL);
Jun 21 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.