首页 \ 问答 \ 我可以让pthread_t实例超出范围吗?(Can I let pthread_t instance go out of scope?)

我可以让pthread_t实例超出范围吗?(Can I let pthread_t instance go out of scope?)

我用C和pthreads编程。 我有一个长期运行的函数,我想在一个单独的线程中运行:

void long_running_function(void * arg) {  
  ...
}


void start_long_running_function(void * arg) {  
  pthread_t thread;  
  pthread_create( &thread , NULL , long_running_function , arg);
  /* What about the thread variable? */  
}  

离开start_long_running_function()函数时,局部变量'thread'将超出范围。 这样可以 - 或者我可以冒险解决问题,例如当long_running_function()完​​成时?

我已经尝试过在我的代码中说明的方法,它似乎有效 - 但也许这只是运气?

关心Joakim


I am programming with C and pthreads. I have a long running function which I want to run in a seperate thread:

void long_running_function(void * arg) {  
  ...
}


void start_long_running_function(void * arg) {  
  pthread_t thread;  
  pthread_create( &thread , NULL , long_running_function , arg);
  /* What about the thread variable? */  
}  

When leaving the start_long_running_function() function the local variable 'thread' will go out of scope. Is this OK - or can I risk problems e.g. when the long_running_function() is complete?

I have tried the approach illustrated in my code, and it seems to work - but maybe that is only luck?

Regards Joakim


原文:https://stackoverflow.com/questions/6855857
更新时间:2022-11-21 08:11

最满意答案

I would try using a while loop to encase the switch. So it keeps asking your input. 
import java.util.Scanner;

public class SwitchTry {

public static void main(String[] args) {

while(true){
    int mth;
    String mthString;
    Scanner scanner = new Scanner(System.in);

    mth =scanner.nextInt();

    switch (mth) {
        case 1: mthString = "January";
            break;
        case 2: mthString = "February";
            break;
        case 3: mthString = "March";
            break;
        case 4: mthString = "April";
            break;
        case 5: mthString = "May";
            break;
        case 6: mthString = "June";
            break;
        case 7: mthString = "July";
            break;
        case 8: mthString = "August";
            break;    
        case 9: mthString = "September";
            break;
        case 10: mthString = "October";
            break;
        case 11: mthString = "November";
            break;
        case 12: mthString = "December";
            break;    
        default: mthString = "Error";
            break;
    }

    System.out.println(mthString);
}
}
}

I would try using a while loop to encase the switch. So it keeps asking your input. 
import java.util.Scanner;

public class SwitchTry {

public static void main(String[] args) {

while(true){
    int mth;
    String mthString;
    Scanner scanner = new Scanner(System.in);

    mth =scanner.nextInt();

    switch (mth) {
        case 1: mthString = "January";
            break;
        case 2: mthString = "February";
            break;
        case 3: mthString = "March";
            break;
        case 4: mthString = "April";
            break;
        case 5: mthString = "May";
            break;
        case 6: mthString = "June";
            break;
        case 7: mthString = "July";
            break;
        case 8: mthString = "August";
            break;    
        case 9: mthString = "September";
            break;
        case 10: mthString = "October";
            break;
        case 11: mthString = "November";
            break;
        case 12: mthString = "December";
            break;    
        default: mthString = "Error";
            break;
    }

    System.out.println(mthString);
}
}
}

相关问答

更多
  • 您可以在inputText上添加.setOnClickListener(),并且当单击inputText时,您将在该部分中执行代码。 在代码运行后,您可以调用另一个事件,并根据需要多次执行此操作。 把这些放在OnCreate方法中: EditText inputText = (EditText) findViewById(R.id.edittextname); inputText.setOnClickListener(action1); OnCreate后,放: public void action2 ( ...
  • 试试这个,因为即使对单个和多个对象也是如此
    • {{relation.name}}
    如果你有一个像下面这样的json,你需要打印Jeremy,Jasmin 看看这个工作示例 $scope.user = [{ 'uId': 1, 'name': ' ...
  • 把你的while循环放在下面的循环中 public static void main(String[] args) { while (true) { Scanner read = new Scanner(System.in); System.out.println("Enter the phone number: "); String telLetter = read.next().toUpperCase(); Str ...
  • 我觉得你不需要任何左连接。 我认为只需要围绕table1而不是table0构建查询。 select t.fatherCode, f.itemName, t.childCode, c.itemName as itemName2, t.childNum from Table1 t join Table0 f on f.itemCode = t.fatherCode join Table0 c on c.itemCode = t.childCode I don't think you ...
  • 显然没有办法绕过它,gamedev教程解释说你只需要在自己的代码中使用自己复杂的解决方法来处理它。 Raw输入我的微软。 当你按住一个键时会发生什么WM_INPUT会发送一个按键消息,等到Windows“按键重复延迟”过去,然后继续以Windows“按键重复速度”设置发送按键信息。 我发现不是使用Makecode或Vkey成员,而是使用keyboard.flags成员更容易,因为(对于大多数键)当键处于关闭状态时,此标志将为零,当它向上时,该标志将设置为1,所以你可以通过以下方式将其反转: switch ...
  • 你可以试试这个。 Select MAX(case when REPORTING_DATE = Convert(datetime, '2016-11-30') then 1 else 0 end) OVER(PARTITION BY ID) as November , Reporting_Date , ID from TableA where 1=1 You can try this. Select MAX(case when REPORTING_DATE = ...
  • I would try using a while loop to encase the switch. So it keeps asking your input. import java.util.Scanner; public class SwitchTry { public static void main(String[] args) { while(true){ int mth; String mthString; Scanner scanner = new Sc ...
  • loop do choice = gets.chomp.upcase case choice when "S", "T", "C", "A", "O" @klass = "#{choice} Klass" @extra = "#{choice} Extra" puts "#{choice} Story" break end end loop do choice = gets.chomp.upcase case choice ...
  • 相关文章

    更多
  • Don’t work. Be hated. Love someone.
  • [转]Top 20 Programming Lessons I've Learned in 20 Years
  • 在oracle9i中如何编写sql或者存储过程来知道执行sql或者函数花费的时间?
  • Lua 文件 I/O 操作
  • Groovy 文件I/O 操作详解
  • gui求jtapi高手T_T
  • 关于如何参与到开源项目中《How To Succeed In Open Source ( In Ways You Haven't Considered Yet )》
  • javax.imageio.IIOException: Can't create output stream!的解决方案
  • I18n的一个问题
  • webservice可不可以实现动态的返回类型:<T> List<T> getList(T a)
  • 最新问答

    更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。