C语言如何像Java一样编写多线程来完成特定任务?可以像objective-c那样完成吗?

发布于 2024-11-04 03:32:38 字数 1552 浏览 1 评论 0原文

我们如何创建多线程或进程?使用C语言跨平台?并在需要时关闭线程。一次编写即可部署Linux/Windows/Mac/Android/MeeGo等平台。

java 中的示例: runnable = new Mytest();线程=新线程(可运行);

Step 1:
    runnable = new Mytest();
    thread = new Thread(runnable);

Step 2:

public class Mytest implements Runnable
{
  private static volatile boolean running = true
  public void run()
  {
    while(running) 
    { // do stuff }
  }
  public void start() { running = true; }
  public void stop()  { running = false;}
}

跟进:

第1步:vim omp_hello.c ( https:/ /computing.llnl.gov/tutorials/openMP/exercise.html )

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) 
{
  int nthreads, tid;
  #pragma omp parallel private(nthreads, tid)
  {

    /* Obtain thread number */
    tid = omp_get_thread_num();
    printf("Hello World from thread = %d\n", tid);

    /* Only master thread does this */
    if (tid == 0) 
    {
      nthreads = omp_get_num_threads();
      printf("Number of threads = %d\n", nthreads);
    }

  }  /* All threads join master thread and disband */

}

步骤 2: $ gcc -fopenmp omp_hello.c -o hello

步骤 3: $ setenv OMP_NUM_THREADS 4

步骤 4:$ ./hello

Hello World from thread = 0 
Number of threads = 4
Hello World from thread = 3
Hello World from thread = 1
Hello World from thread = 2

完成。有用!

How do we create multi thread or process? For cross platform using C language? And close the thread when it requires. Write once and deploy platforms such as Linux/Windows/Mac/Android/MeeGo.

Example in java: runnable = new Mytest(); thread = new Thread(runnable);

Step 1:
    runnable = new Mytest();
    thread = new Thread(runnable);

Step 2:

public class Mytest implements Runnable
{
  private static volatile boolean running = true
  public void run()
  {
    while(running) 
    { // do stuff }
  }
  public void start() { running = true; }
  public void stop()  { running = false;}
}

Follow up:

Step 1: vim omp_hello.c ( https://computing.llnl.gov/tutorials/openMP/exercise.html )

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) 
{
  int nthreads, tid;
  #pragma omp parallel private(nthreads, tid)
  {

    /* Obtain thread number */
    tid = omp_get_thread_num();
    printf("Hello World from thread = %d\n", tid);

    /* Only master thread does this */
    if (tid == 0) 
    {
      nthreads = omp_get_num_threads();
      printf("Number of threads = %d\n", nthreads);
    }

  }  /* All threads join master thread and disband */

}

Step 2: $ gcc -fopenmp omp_hello.c -o hello

Step 3: $ setenv OMP_NUM_THREADS 4

Step 4: $ ./hello

Hello World from thread = 0 
Number of threads = 4
Hello World from thread = 3
Hello World from thread = 1
Hello World from thread = 2

Done. it Works!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归途 2024-11-11 03:32:38

C语言中没有线程的概念。大多数平台都有自己特定的线程库或API(显然有些平台根本不支持线程)。

但是,存在诸如 OpenMP 之类的第 3 方库,它们在一定程度上提供了平台无关的线程。

There is no concept of threading in the C language. Most platforms have their own specific libraries or API for threading (obviously some platforms can't support threading at all).

However, 3rd-party libraries such as OpenMP exist, which offer platform-independent threading, to an extent.

孤寂小茶 2024-11-11 03:32:38

在 C++ 中,您需要使用 Boost Thread 库 - 它是跨平台的,易于使用,而且非常可靠。我不知道有类似的 C 库。

线程纯粹是 C 和 C++ 中的库问题,特别是因为它们在每个平台上的实现方式都不同。

In C++, you will want to use the Boost Thread library - it's cross-platform, easy to use, and really solid. I don't know of a similar library for C.

Threads are purely a library issue in C and C++, specifically because they're implemented differently on every platform.

阳光下的泡沫是彩色的 2024-11-11 03:32:38

对于C语言,您可以使用pthread,但是对于跨平台开发,这取决于您解决具体问题对于每个操作系统。

With C language you can use pthread but for what concerning croossplatform developing, it's up to you resolving specific issue for every operating system.

皇甫轩 2024-11-11 03:32:38

wxWidget 是多平台的(Linux/Windows/Mac 上的 C++),并且有它的特殊线程类:wxThread(多平台)。顺便说一下,GUI 也是多平台的。
我不明白如何使用不同的语言部署一次(android 是 Java,你想要 c++?)

wxWidget is multiplatform(C++ on Linux/Windows/Mac) and has it's special thread class : wxThread (multiplatform). GUI is also multiplatform by the way.
I don't see how you can deploy once using different languages (android is Java and you want c++?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文